package org.example;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class WebTest {
    static WebDriver webDriver;

//    @BeforeAll
//    static void Connect(){
//        webDriver = new ChromeDriver();
//        webDriver.get("http://120.26.37.204:8088/marks/login/");
//    }
    @ParameterizedTest
    @CsvFileSource(resources = "full_coverage_test.csv")
    void LoginSuccess(String user, String password, String target_url) throws InterruptedException {
        webDriver = new ChromeDriver();
        webDriver.get("http://120.26.37.204:8088/marks/login/");
        System.out.println("用户名: " + user + " 密码: " + password);
        //隐式等待3秒，加载页面
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //定位用户输入框和密码框，输入用户名和密码
        WebElement user_in = webDriver.findElement(By.cssSelector("body > div.login-container > div > div.login-form > form > div:nth-child(1) > input[type=text]"));
        WebElement pass_in = webDriver.findElement(By.cssSelector("body > div.login-container > div > div.login-form > form > div:nth-child(2) > input[type=password]"));
        user_in.sendKeys(user);
        pass_in.sendKeys(password);
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#loginFormBtn")).click();
        //校验 ———— 判断页面是否跳转
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        sleep(2000);
        String cur_url = webDriver.getCurrentUrl();
        System.out.println("cur_url: " + cur_url);
        Assertions.assertEquals(target_url, cur_url);
    }
}
